//
//  TestAfnetHttp.m
//  TestAfnetwork
//
//  Created by xujingjing on 16/6/15.
//  Copyright © 2016年 xujingjing. All rights reserved.
//

#import "TestAfnetHttp.h"


@implementation TestAfnetHttp
//NSData *JsonData(id object){
//    NSError * error = nil;
//    NSData * jsonData = [NSJSONSerialization dataWithJSONObject:object options:NSJSONWritingPrettyPrinted error:&error];
//    
//    if ([jsonData length] != 0 && error == nil){
//        return jsonData;
//    }else{
//        NSLog(@"%@ Failed to Convert to JsonData",object);
//        return nil;
//    }
//    
//}
//NSString * JsonString(id target){
//    NSData * jsonData = JsonData(target);
//    if (jsonData) {
//        NSString * jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
//        return jsonString;
//    }
//    return nil;
//}
//

+(void)postWithUrl:(NSString *)urlstr paramters:(NSDictionary *)paramters success:(void (^)(id))success failed:(void (^)(id))failed{


    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer.timeoutInterval = 5;
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [manager.requestSerializer setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [manager.requestSerializer setValue:API_KEY forHTTPHeaderField:@"apikey"];
   // manager.responseSerializer = [AFJSONResponseSerializer serializer];//声明返回的结果是json类型
   
    //set cachePolicy
    manager.requestSerializer.cachePolicy = NSURLRequestReturnCacheDataElseLoad;
    
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json",@"text/javescript",@"text/plain",@"text/html", nil];
        
    [manager POST:urlstr parameters:paramters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
       
        success(responseObject);

   } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
       
       NSLog(@"————————————————————》error%@",error);

   }];
    
    
}

@end
